# encoding:utf8
"""
@File        : yhyx.py
@Time        : 2019/6/25 11:39
@Author      : zhaoy
@Email       : zhaoyao@shandiangou.cc
@Description : 一禾优选社区
抓取范围：
    ["惠州市", "济宁市", "青岛市", "上海市", "铁岭市", "渭南市", "烟台市", "重庆市", "镇江市"]
    目前就开通了上面这些城市.
"""

import json

import pymongo
import scrapy

from .citys import shequren_city_info
from ..settings import *


class YiHeYouXuan(scrapy.Spider):
    name = 'yhyx'
    custom_settings = {
        'DEFAULT_REQUEST_HEADERS': {
            'Host': "yd.frxs.cn",
            'Connection': "keep-alive",
            'Accept': "*/*",
            'User-Agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 "
                          "(KHTML, like Gecko) Mobile/14G60 MicroMessenger/7.0.3(0x17000321) NetType/WIFI Language/zh_CN",
            'Accept-Language': "zh-cn",
        },
        'IMAGES_STORE': 'G:/spiders_data/images/xsyx/',
        'ITEM_PIPELINES': {
            'baiguoyuan.pipelines.GoodsImagesPipeline': 1,
            'baiguoyuan.pipelines.CommomPipelines': 321
        },
    }
    allowed_domains = ['frxs.cn']

    def __init__(self):
        self.client = pymongo.MongoClient(host=MONGO_URI)
        self.db = self.client[MONGO_DATABASE]
        self.collection = 'grids'
        super(YiHeYouXuan, self).__init__()

    def start_requests(self):
        bgy_citymap = {}
        [bgy_citymap.update({ct.get('name'): ct.get('cityCode')}) for ct in
         shequren_city_info.get('_embedded').get('content')]
        locations = self.db.get_collection(self.collection).find({'city': {
            '$in': ["惠州市", "济宁市", "青岛市", "上海市",
                    "铁岭市", "渭南市", "烟台市", "重庆市", "镇江市"]}},
            {'city': 1, 'center_lat': 1, 'center_lng': 1})
        url = 'https://yd.frxs.cn/api/user/store/getNearStoreList'
        for loc in locations:
            data = {
                'mapX': loc.get('center_lng'),
                'mapY': loc.get('center_lat'),
            }
            yield scrapy.Request(url, callback=self.parse_store_list, method='POST', body=json.dumps(data))

    def parse_store_list(self, response):
        data = response.text
        data = json.loads(data)
        data.update({'data_collection': 'xsyx_stores', 'data_from': 'data'})
        yield data
        _data = data.get('data')
        if _data:
            for dt in _data:
                dct = dict()
                dct['storeId'] = dt.get('storeId')
                dct['areaId'] = dt.get('areaId')
                url = 'https://mall.xsyxsc.com/user/product/indexData'
                yield scrapy.Request(url, method='POST', body=json.dumps(dct))
        else:
            return

    def parse(self, response):
        data = response.text
        data = json.loads(data)
        image_urls = []
        image_header = {
            'Accept': "image/*;q=0.8",
            'Accept-Language': "zh-cn",
            'Connection': "keep-alive",
            'Accept-Encoding': "gzip, deflate",
            'Cache-Control': "no-cache",
        }
        if data:
            image_urls = [(item.get('adUrl'), item.get('vesName')) for item in data]
        data.update({'data_collection': 'xsyx_goods', 'data_from': 'data.products', 'image_urls': image_urls,
                     'image_header': image_header})
        yield data
